package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.service.UserAccountService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/api/core/userAccount")
public class ApiUserAccountController {
    @Resource
    UserAccountService userAccountService;
    //http://192.168.1.171/api/core/userAccount/notifyWithdraw
    @ApiOperation("提现回调接口")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(@RequestParam Map<String,Object> map){
        return userAccountService.notifyWithdraw(map);
    }

    //封装自动提交提现申请到hfb平台的form表单字符串
    @ApiOperation("提现申请")
    @PutMapping("withDraw/{amount}")
    public ResponseVo withDraw(@RequestHeader(value = "token",required = false)String token,
                               @PathVariable("amount")BigDecimal amount){
        String form = userAccountService.withDraw(token,amount);
        return ResponseVo.ok().data("form",form);
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/amount")
    public ResponseVo amount(@RequestHeader(value = "token",required = false)String token){
        Long userId = JwtUtils.getUserId(token);
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class)
                .eq(UserAccount::getUserId, userId)
                .select(UserAccount::getAmount));
        return ResponseVo.ok().data("amount",userAccount.getAmount());
    }

    @ApiOperation("构建自动提交的充值表单")
    // 表单自动提交的地址: http://localhost:9999/userAccount/AgreeBankCharge
    @PostMapping("charge")
    public ResponseVo charge(BigDecimal amount,
                             @RequestHeader(value = "token",required = false)String token){

        String formStr = userAccountService.charge(amount,token);
        return ResponseVo.ok().data("form",formStr);
    }

    @ApiOperation("充值完成后的回调接口")
    //http://192.168.1.171/api/core/userAccount/notify
    @PostMapping("notify")
    //@RequestParam Map<String,Object> map 表示解析请求参数存到map中
    // Map<String,Object> map  表示request域  和 Model model
    public String rechargeNotify(@RequestParam Map<String,Object> map){
        //System.out.println(map);
        return userAccountService.rechargeNotify(map);
    }
}

